<template>
  <div class="class-dialog">
    <div class="dialog-wrapper">
      <div class="dialog-close flex-center" @click="closeDialog">
        <zl-icon
          class="dialog-close-icon"
          icon="icon-guanbi"
          :icon-style="{ width: '20px', height: '20px' }"
        ></zl-icon>
      </div>
      <div class="dialog-title">知识点切换</div>
      <div class="mt20 site-teacher-top flex-center-wrap">
        <div class="site-teacher-label">换个老师讲解</div>
        <div class="site-teacher-name text-ess-1">
          {{ currentSite.exam_site_desc || '' }}
        </div>
      </div>
      <template v-if="isArrAndLen(teacherList)">
        <div class="mt16 flex-wrap flex-multi-row">
          <div
            :class="[
              'flex-center',
              'flex-column-wrap',
              'site-teacher-item',
              teacherIndex === index ? 'site-teacher-item-active' : ''
            ]"
            v-for="(item, index) in teacherList"
            @click="changeTeacher(index)"
            :key="index"
          >
            <img
              class="site-teacher-item-img"
              :src="parseResourceUrl(item.teacher_img)"
              alt=""
              srcset=""
            />
            <div class="site-teacher-item-name">{{ item.teacher_name }}</div>
            <img
              class="site-teacher-checked"
              src="//image.zlketang.com/public/news/common_study/site_teacher_checked.png"
            />
          </div>
        </div>
        <div class="flex-center">
          <div class="site-teacher-btn flex-center" @click="closeDialog">
            取消
          </div>
          <div
            class="site-teacher-btn site-teacher-btn-sure flex-center"
            @click="sureTeacher"
          >
            确定
          </div>
        </div>
      </template>
      <div v-else class="flex-center flex-column-wrap">
        <img
          class="no-teacher-img"
          src="//image.zlketang.com/public/news/common_study/no_teacher.png"
        />
        <div class="no-teacher-tip">当前知识点暂无老师哦</div>
      </div>
    </div>
  </div>
</template>
<script>
import { getSiteTeacher } from '@/api/play'
import zlIcon from '@/components/zlIcon.vue'
export default {
  components: {
    'zl-icon': zlIcon
  },
  data() {
    return {
      //老师列表
      teacherList: [],
      //当前选中的老师索引
      teacherIndex: 0,
      zlaBtn: {
        key_name: '',
        key_content: ''
      }
    }
  },
  computed: {
    // 播放中的课节
    playingSection() {
      return this.$store.state.play.playingSection || {}
    },
    //当前正在播放的知识点
    currentSite() {
      return this.$store.state.play.currentSite || {}
    },
    // 当前权限选中的信息
    selectInfo() {
      return this.$store.state.play.selectInfo || {}
    }
  },
  watch: {
    // 之了埋点
    zlaBtn: {
      handler() {
        this.sendZlaClick()
      },
      deep: true
    }
  },
  methods: {
    // 之了数据点击
    sendZlaClick() {
      this.keyClick({
        ...this.zlaBtn
      })
    },
    // 关闭弹窗
    closeDialog() {
      this.$emit('close')
    },
    changeTeacher(index) {
      this.teacherIndex = index
    },
    //确定选中的老师
    sureTeacher() {
      this.zlaBtn = {
        key_name: '切换老师',
        key_content: this.playingSection.section_name || ''
      }
      setTimeout(() => {
        this.$emit('sure-teacher', this.teacherList[this.teacherIndex])
      }, 300)
    },
    getTeacherList() {
      let { course_id, course_section_id, teacher_id } = this.selectInfo
      let { exam_site_id } = this.currentSite
      if (!exam_site_id) return
      let params = {
        course_id,
        course_section_id,
        exam_site_id
      }
      getSiteTeacher(params).then(({ data = [] }) => {
        this.teacherList = data
        this.teacherList = this.teacherList.filter(
          item => +item.teacher_id !== +teacher_id
        )
        // console.log(11111, this.teacherList)
      })
    }
  },
  mounted() {
    // console.log(2222, this.selectInfo)
    this.getTeacherList()
  }
}
</script>
<style scoped>
.mt20 {
  margin-top: 20px;
}

.class-dialog {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  z-index: 999;
}

.dialog-close {
  position: absolute;
  z-index: 9;
  right: 20px;
  top: 20px;
  cursor: pointer;
}

.dialog-close-icon {
  font-size: 24px;
  color: #999999;
}

.dialog-close-icon:hover {
  color: #666666;
}

.dialog-wrapper {
  box-sizing: border-box;
  position: absolute;
  z-index: 9;
  top: 50%;
  left: 50%;
  padding: 20px 30px;
  width: 450px;
  -webkit-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
  border-radius: 12px;
  background: #fff;
}

.dialog-title {
  height: 28px;
  line-height: 28px;
  font-size: 20px;
  font-weight: bold;
  text-align: center;
  color: #383838;
}

.site-teacher-top {
  font-size: 16px;
  color: #999999;
  line-height: 22px;
}

.site-teacher-label {
  flex-shrink: 0;
}

.site-teacher-name {
  margin-left: 4px;
  color: #3480ff;
}

.site-teacher-item {
  position: relative;
  box-sizing: border-box;
  margin: 0 10px 20px 0;
  width: 90px;
  height: 108px;
  background: linear-gradient(360deg, #f5f9ff 0%, #fafcff 100%);
  border-radius: 8px;
  cursor: pointer;
}

.site-teacher-item:nth-child(4n) {
  margin-right: 0;
}

.site-teacher-item-active {
  border: 1px solid #b3cfff;
}

.site-teacher-item-img {
  width: 48px;
  height: 48px;
  background: #d9d9d9;
  border-radius: 50%;
}

.site-teacher-item-name {
  margin-top: 6px;
  font-size: 16px;
  color: #383838;
  line-height: 22px;
}

.site-teacher-checked {
  display: none;
  position: absolute;
  right: 0;
  bottom: 0;
  width: 20px;
  height: 19px;
}

.site-teacher-item-active .site-teacher-item-name {
  font-weight: 500;
  color: #3480ff;
}

.site-teacher-item-active .site-teacher-checked {
  display: block;
}

.no-teacher-img {
  margin-top: 41px;
  width: 122px;
  height: 75px;
}

.no-teacher-tip {
  margin: 10px 0 50px 0;
  font-size: 14px;
  color: #999999;
  line-height: 20px;
}

.site-teacher-btn {
  width: 86px;
  height: 32px;
  font-size: 14px;
  color: #666666;
  border-radius: 18px;
  border: 1px solid #d9d9d9;
  cursor: pointer;
}

.site-teacher-btn-sure {
  margin-left: 12px;
  color: #fff;
  background: #3480ff;
  border: none;
}
</style>
